const { app, BrowserWindow } = require('electron')
const path = require('path')

function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    menuBarVisible:false,
    webPreferences: {
        sandbox:true,
        nodeIntegration:false,
        preload: path.join(__dirname, 'preload.js')
    },
  })
  
    // win.setMenu(null)
    win.loadURL(`file://${__dirname}/nodepad/dist/index.html`);
    // win.loadURL(`http://127.0.0.1:8080/`);
    //   win.loadFile('index.html')
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})


const { ipcMain } = require('electron')

const dbFile = require('./db')

ipcMain.handle('test',(ev,targ)=>{
    console.log("test rec",targ)
})

// const fs = require('fs');

// if (!fs.existsSync(__dirname+"\\doc")){
//     fs.mkdirSync(__dirname+"\\doc")
// }
// if (!fs.existsSync(__dirname+"\\doc")){
//     console.log("文件夹创建错误")
// }
// var line = fs.readdirSync(__dirname+"\\doc")

const { dialog } = require('electron')
const fs = require('fs')
var db = null

ipcMain.handle('app',(event,msgid,arg1,arg2,arg3)=>{
    console.log('app',msgid,arg1,arg2,arg3,arg3)

    if(msgid=="save_or_open_file"){
        var path = dialog.showOpenDialogSync({ properties: ['promptToCreate'] })
        event.sender.send('save_or_open_file_notify',path)
    }
    if(msgid=="exists_file"){
        var exists =fs.existsSync(arg1)
        event.sender.send('exists_file_notify',exists)
    }
    if(msgid=="select_db"){
        var path = arg1
        if(db!=null){
            db.close()
        }
        db = new dbFile.NodeBook(path,()=>{
            db.all((tree)=>{
                event.sender.send('load_db',tree)
            })
        })
    }
    if(msgid=="search_db"){
        //搜索！
        var keyword = arg1
        db.all_search(keyword,(tree)=>{
            event.sender.send('load_db',tree)
        })
    }
    if(msgid=="get_note"){
        db.note_get(arg1,(row,err)=>{
            event.sender.send('load_note',row,err)
        })
    }
    if(msgid=="set_note"){
        db.note_set(arg1.note_id,arg1.notebook_id,arg1.name,(err)=>{
            event.sender.send('set_note_notify',err)
        })
    }
    if(msgid=="set_note_text"){
        db.note_set_text(arg1,arg2,(err)=>{
            event.sender.send("set_note_text_notify",err)
        })
    }
    if(msgid=="set_notebook"){
        db.notebook_set(arg1.notebook_id,arg1.parent_id,arg1.name,(err)=>{
            event.sender.send('set_notebook_notify',err)
        })
    }
    if(msgid=="rem_note"){
        db.note_rem(arg1,(err)=>{
            event.sender.send('rem_note_notify',err)
        })
    }
    if(msgid=="rem_notebook"){
        db.notebook_rem(arg1,(err)=>{
            event.sender.send('rem_notebook_notify',err)
        })
    }
})
